package org.zjx.app.action.window;

import org.zjx.analysis.analyze.window.impl.DialogueAnalyzer;
import org.zjx.analysis.analyze.window.impl.ShoppingAnalysis;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.app.action.gather.GatherPropAction;
import org.zjx.command.entity.ItemTemplate;
import org.zjx.data.prop.ItemTemplateData;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.List;

public class ShoppingAction extends AbstractWindowAction {
    private ShoppingAnalysis shoppingAnalysis;

    private Rectangle itemArea;

    private final String itemName;
    private final int buyNumber;

    private void analysis() {
        shoppingAnalysis = new ShoppingAnalysis(targetWindow);
    }

    public ShoppingAction(IWindow targetWindow, String itemName, int buyNumber) {
        super(targetWindow, false);
        this.itemName = itemName;
        this.buyNumber = buyNumber;
    }

    @Override
    public void open() {

    }

    @Override
    public void close() {
        if (!shoppingAnalysis.isOpened()) return;
        Rectangle closeButtonPoint = shoppingAnalysis.getCloseButtonPoint();
        targetWindow.clickIn(shoppingAnalysis.getCloseButtonPoint());
        mouseAloofWindow(closeButtonPoint);
        targetWindow.execute(WindowOperation.delay(100));
        targetWindow.flushScreenshot();
        close();
    }

    @Override
    public void reSetWindow() {
        if (shoppingAnalysis.isTransborder()) {
            Rectangle from = shoppingAnalysis.conversion(shoppingAnalysis.windowArea, shoppingAnalysis.getRefImageArea());
            Rectangle to = shoppingAnalysis.conversion(shoppingAnalysis.getOriginBounds(), shoppingAnalysis.getRefImageArea());
            targetWindow.actions().drag(from.x, from.y, to.x, to.y).delay(100);
            mouseAloofWindow(shoppingAnalysis.getOriginBounds());
        }
    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (shoppingAnalysis.mouseInRect(rect)) {
            Point safePoint = shoppingAnalysis.findLargestRemainingAreaCenter(rect);
            targetWindow.moveTo(safePoint.x, safePoint.y);
            targetWindow.execute(WindowOperation.delay(100));
            targetWindow.flushScreenshot();
            analysis();
        }
    }

    @Override
    protected void doExecute() throws Exception {
        analysis();
        reSetWindow();
        mouseAloofWindow(shoppingAnalysis.windowArea == null ? shoppingAnalysis.getOriginBounds() : shoppingAnalysis.windowArea);
        if (!shoppingAnalysis.isOpened()) return;
        updateItemArea();

        while (itemArea != null && shoppingAnalysis.getBuyCount() < buyNumber) {
            int off = buyNumber - shoppingAnalysis.getBuyCount();
            while (off-- > 0){
                targetWindow.actions().clickIn(itemArea).delay(50);
            }
            targetWindow.actions().delay(100).moveTo(100,200).delay(200);
            targetWindow.flushScreenshot();
            analysis();
        }

        int cashMoney = shoppingAnalysis.getCashMoney();
        int buyMoney = shoppingAnalysis.getBuyMoney();
        if (cashMoney < buyMoney) {
            close();
            throw new IllegalStateException("购买道具余额不足!!!");
        }

        Rectangle buyButtonArea = shoppingAnalysis.conversion(shoppingAnalysis.windowArea,shoppingAnalysis.buyButtonArea());
        targetWindow.actions().clickIn(buyButtonArea).delay(500);
        targetWindow.flushScreenshot();

        DialogueAnalyzer dialogueAnalysis = new DialogueAnalyzer(targetWindow);
        while (dialogueAnalysis.isOpened()){
            targetWindow.actions().clickIn(dialogueAnalysis.getCloseButtonPoint()).delay(200);
            targetWindow.flushScreenshot();
            dialogueAnalysis = new DialogueAnalyzer(targetWindow);
        }
        close();
    }

    private void updateItemArea() {
        List<ItemTemplate> itemTemplates = matchItemTemplates(shoppingAnalysis.getItemImages());
        for (int i = 0; i < itemTemplates.size(); i++) {
            ItemTemplate itemTemplate = itemTemplates.get(i);
            if (itemTemplate == null) continue;
            if (itemTemplate.getName().equals(itemName)) {
                itemArea = shoppingAnalysis.getItemIndexArea(i);
                return;
            }
        }
    }


    /**
     * 匹配物品模板
     */
    private List<ItemTemplate> matchItemTemplates(List<BufferedImage> imageStrings) {
        if (imageStrings == null || imageStrings.isEmpty()) {
            return Collections.emptyList();
        }
        return ItemTemplateData.getItemTemplateData(imageStrings);
    }
}
